﻿// -----------------------------------------------------------------------
// <copyright file="Comparer.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Sitecore.ChangesTracker.UI
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using Sitecore.Shell.Framework.Commands;
  using Sitecore.Data.Items;
  using Sitecore.ChangesTracker.Archiving;
  using Sitecore.Diagnostics;
  using Sitecore.Web;
  using Sitecore.Data.Archiving;
  using Sitecore.Web.UI.WebControls.Ribbons;
  using Sitecore.Shell.Applications.ContentEditor;
  using Sitecore.Reflection;
  using Sitecore.Web.UI.HtmlControls;
  using Sitecore.Shell.Applications.ContentManager;
  using Sitecore.Data.Validators;
  using Sitecore.Xml;
  using System.Collections;
  using Sitecore.Web.UI.Sheer;
  using Sitecore.Text;
  using Sitecore.Data;
  using Sitecore.Configuration;
  using Sitecore.Collections;
  using Sitecore.StringExtensions;
  using Sitecore.Globalization;
  using Sitecore.Shell.Applications.ContentManager.ReturnFieldEditorValues;
  using Sitecore.Pipelines.Save;
  using Sitecore.Web.Configuration;
  using Sitecore.Shell;
  using Sitecore.Web.UI.XamlSharp.Continuations;

  
 
  /// <summary>
  /// TODO: Update summary.
  /// </summary>
  public class Comparer :Sitecore.Shell.Web.UI.SecurePage, IHasCommandContext, ISupportsContinuation 
  {
    // Fields
    protected System.Web.UI.HtmlControls.HtmlGenericControl Body;
    protected System.Web.UI.WebControls.PlaceHolder BrowserTitle;
    //protected System.Web.UI.WebControls.Literal Buttons;
    protected Border ContentEditor;
    protected System.Web.UI.HtmlControls.HtmlForm ComparerForm;
    protected ThemedImage DialogIcon;
    protected Literal DialogText;
    protected Literal DialogTitle;
    protected System.Web.UI.WebControls.PlaceHolder DocumentType;
    protected Literal Literal1;
    protected System.Web.UI.WebControls.PlaceHolder scLanguage;
    protected System.Web.UI.WebControls.PlaceHolder Stylesheets;
    protected Ribbon ribbon;
    protected ArchivedItem arcItem;
    protected Item realItem;
    

   
    // Methods
    protected override void OnPreRender(EventArgs e)
    {
      
      base.OnPreRender(e);
      if (base.CodeBeside != null)
      {
        string id = WebUtil.GetQueryString("arc_id");
       
        string langdb = WebUtil.GetSessionString("arc_lang_db", Sitecore.Context.ContentLanguage.Name);
        string versiondb = WebUtil.GetSessionString("arc_ver_db", "0");
        string langcp = WebUtil.GetSessionString("arc_lang_cp", Sitecore.Context.ContentLanguage.Name);
        string versioncp = WebUtil.GetSessionString("arc_ver_cp", "0");
        ComparerEventArgs arg = new ComparerEventArgs(new ViewItemInfo(langdb,Convert.ToInt32(versiondb)), new ViewItemInfo(langcp, Convert.ToInt32(versioncp)), new Guid(id));

        ReflectionUtil.CallMethod(base.CodeBeside, "OnPreRendered", true, true, new object[] { arg });
      }
    }
     

    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);
      if (!base.IsTrackingViewState && !Sitecore.Web.UI.ViewStateDisabler.IsActive)
      {
        this.TrackViewState();
      }
      this.EnsureChildControls();
      string str = this.ViewState["ContinuationKey"] as string;
      if (string.IsNullOrEmpty(str))
      {
        str = Sitecore.Data.ID.NewID.ToShortID().ToString();
        this.ViewState["ContinuationKey"] = str;
      }
      Sitecore.Web.UI.WebControls.ContinuationManager.Register(str, this);

    }


    #region IHasCommandContext Members

    public CommandContext GetCommandContext()
    {
        string str = "/sitecore/content/Applications/Archives/Compare Items/Ribbon";
        Item itemNotNull = Client.GetItemNotNull(str, Client.CoreDatabase);
        CommandContext context = new CommandContext();
        string id = WebUtil.GetQueryString("arc_id");
        context.Parameters["archivename"] = WebUtil.GetQueryString("archivename", "changetracker");
        Assert.ArgumentNotNullOrEmpty(id, "empty strings are not allowed, id");
        WebUtil.SetSessionValue("arc_id", id);
        context.Parameters["arc_id"] = id;        
        arcItem = ((ChangeTrackerArchive)ArchiveManager.Providers["changetracker"].GetArchive("changetracker", Sitecore.Context.ContentDatabase)).GetArchivedItems(new Guid(id)).FirstOrDefault();
        Assert.IsNotNull(arcItem, "Archived item not found, archivalId:" + id);
        string langdb = WebUtil.GetSessionString("arc_lang_db", Sitecore.Context.ContentLanguage.Name);
        context.Parameters["arc_lang_db"] = langdb;
        string versiondb = WebUtil.GetSessionString("arc_ver_db", "0");
        context.Parameters["arc_ver_db"] = versiondb;
        realItem = Sitecore.Context.ContentDatabase.GetItem(Sitecore.Data.ID.Parse(arcItem.ItemId),Language.Parse(langdb),Sitecore.Data.Version.Parse(versiondb));
        if (realItem != null)
        {
          WebUtil.SetSessionValue("itm_id", realItem.ID.ToString());
          context.Parameters["itm_id"] = realItem.ID.ToString();         
        }
        context.RibbonSourceUri = itemNotNull.Uri;           
      return context;
    }

    #endregion
  }

 
}
